import { createRouter, createWebHistory } from "vue-router";
import index from "../indexView.vue";
import userRoutes from "./uesrRoutes"
import goodsRoutes from "./goodsRoutes"
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "index",
      component: index,
      children: [
        {
          path: "",
          name: "index",
          component: () => import("../views/user/index.vue"),
          meta: {
            index: 0,
            cache: true //设置页面是否需要使用缓存
          }
        },
        {
          path: "/sort",
          name: "sort",
          component: () => import("../views/user/sortView.vue"),
          meta: {
            index: 1,
            cache: true
          }
        },
        {
          path: "/shopping",
          name: "shopping",
          component: () => import("../views/user/shoppingCart.vue"),
          meta: {
            index: 2
          }
        },
        {
          path: "/my",
          name: "my",
          component: () => import("../views/user/myView.vue"),
          meta: {
            index: 3,
            cache: true
          }
        },
      ],
    },
    ...goodsRoutes,
    ...userRoutes,
    {
      path: "/search",
      name: "search",
      component: () => import("../views/user/searchView.vue"),
    },
  ],
});
//全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to, from, next) => {
  //如果路由需要跳转
  if (to.meta.isAuth) {
    //判断 如果school本地存储是qinghuadaxue的时候，可以进去
    if (localStorage.getItem('uid ')) {
      next()  //放行
    } else {
      alert('抱歉，您无权限查看！')
    }
  } else {
    // 否则，放行
    next()
  }
})
export default router;
